﻿using System;
using System.Web;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
namespace Avatar.BLL
{
    /// <summary>
    /// 文章采集类库 Csb V 1.0
    /// </summary>
    public class Collection
    {
        
        WebClient wc = new WebClient();
        Stream stream;
        StreamReader sr;
        #region 私有变量
        private string _bodystart;
        private string _bodyend;
        private string _coding;
        private string _htmlcode;
        #endregion

        #region 属性
        /// <summary>
        /// 采集内容起始位置
        /// </summary>
        public string BodyStart
        {
            get { return _bodystart; }
            set { _bodystart = value; }
        }
        /// <summary>
        /// 采集内容结束位置
        /// </summary>
        public string BodyEnd
        {
            get { return _bodyend; }
            set { _bodyend = value; }
        }
        /// <summary>
        /// 采集内容编码设置
        /// </summary>
        public string Coding
        {
            get {return _coding; }
            set { _coding = value; }
        }
        public string HTMLCode
        {
            get { return _htmlcode; }
        }
        public Collection()
        {
            //Command.sqlCommand();
        }
        #endregion

        /// <summary>
        /// 返回html代码
        /// </summary>
        /// <param name="_url"></param>
        /// <returns></returns>
        public string ReaderHTMLCode(string _url) {
            string _htmlCode;
            // 获取循环页面的HTML源代码
            try {
                stream = wc.OpenRead(_url);
                sr = new StreamReader(stream, Encoding.GetEncoding(_coding));
                // 取得最后的HTML源代码
                _htmlCode = sr.ReadToEnd();
                sr.Close();
                sr.Dispose();
                return _htmlCode;
            } catch {
                return "";
            }
        }

        /// <summary>
        /// 返回采集文章的匹配项的值
        /// </summary>
        /// <param name="_pageurl">需要采集的页面地址规则</param>
        /// <param name="i">循环读取页面序号值</param>
        /// <returns>返回匹配值，如有错误，则返回错误信息</returns>
        public string Collection_Article_Start(string _pageurl,int i)
        {
            string message = null;
            int texts = -1, texte = -1;

            int pagestart = _pageurl.IndexOf("{");
            int pageend = _pageurl.IndexOf("}");
            string newurl = _pageurl.Substring(0, pagestart);
            string newurl2 = _pageurl.Substring(pageend + 1);
            try {
                // 获取循环页面的HTML源代码
                stream = wc.OpenRead(newurl + i + newurl2);
                sr = new StreamReader(stream, Encoding.GetEncoding(_coding));
                // 取得最后的HTML源代码
                string textbx = sr.ReadToEnd();
                _htmlcode = ReplaceEnter(textbx);
                texts = _htmlcode.IndexOf(ReplaceEnter(_bodystart));
                texte = _htmlcode.IndexOf(ReplaceEnter(_bodyend));
                message = _htmlcode.Substring(texts + _bodystart.Length, texte - texts - _bodystart.Length);
                sr.Close();
                sr.Dispose();
            } catch {
                message = "无法打开页面。" + newurl + i + newurl2 + "<br />";
            } finally {
                if (texts == -1 || texte == -1) {
                    message = "无法打开页面。" + _pageurl + "<br />";
                }
            }
            return message;
        }

        /// <summary>
        /// 返回采集文章的匹配项的值
        /// </summary>
        /// <param name="_pageurl">需要采集的页面地址规则</param>
        /// <returns>返回匹配值，如有错误，则返回错误信息</returns>
        public string Collection_Article_Start(string _pageurl)
        {
            string message = null;
            int texts = -1, texte = -1;
            try {
                // 获取循环页面的HTML源代码
                Stream stream = wc.OpenRead(_pageurl);
                StreamReader sr = new StreamReader(stream, Encoding.GetEncoding(_coding));
                // 取得最后的HTML源代码
                string textbx = sr.ReadToEnd();
                _htmlcode = ReplaceEnter(textbx);
                texts = _htmlcode.IndexOf(ReplaceEnter(_bodystart));
                texte = _htmlcode.IndexOf(ReplaceEnter(_bodyend));
                message = _htmlcode.Substring(texts + _bodystart.Length, texte - texts - _bodystart.Length);
                sr.Close();
                sr.Dispose();
            } catch {
                message = "无法打开页面。" + _pageurl + "<br />";
            } finally {
                if (texts == -1 || texte == -1) {
                    message = "无法打开页面。" + _pageurl + "<br />";
                }
            }
            return message;
        }

        /// <summary>
        /// 返回采集职位的匹配项的值
        /// </summary>
        /// <param name="_content">HTML源文件代码</param>
        /// <returns>返回匹配值，如有错误，则返回错误信息</returns>
        public string Collection_Job_Start(string _content) {
            string message = null;
            int texts = -1, texte = -1;
            try {
                _htmlcode = ReplaceEnter(_content);
                texts = _htmlcode.IndexOf(ReplaceEnter(_bodystart));
                // 取得从开始截取字符串后的第一个标签
                texte = _htmlcode.IndexOf(ReplaceEnter(_bodyend));
                message = _htmlcode.Substring(texts + _bodystart.Length, texte - texts - _bodystart.Length);
            } catch {
                message = "";
            } finally {
                if(texts == -1 || texte == -1) {
                    message = "";
                }
            }
            return message;
        }
        /// <summary>
        /// 返回采集职位的匹配项的值
        /// </summary>
        /// <param name="_content">HTML源文件代码</param>
        /// <returns>返回匹配值，如有错误，则返回错误信息</returns>
        public string Collection_Job_End(string _content) {
            string message = null;
            int texts = -1, texte = -1;
            try {
                _htmlcode = ReplaceEnter(_content);
                texts = _htmlcode.IndexOf(ReplaceEnter(_bodystart));
                // 取得从开始截取字符串后的第一个标签
                texte = _htmlcode.LastIndexOf(ReplaceEnter(_bodyend));
                message = _htmlcode.Substring(texts + _bodystart.Length, texte - texts - _bodystart.Length);
            } catch {
                message = "";
            } finally {
                if(texts == -1 || texte == -1) {
                    message = "";
                }
            }
            return message;
        }

        /// <summary>
        /// 替换HTML里的各种回车，单引号
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        protected string ReplaceEnter(string text)
        {
            text = text.Replace("\r\n", "");
            text = text.Replace("\r", "");
            text = text.Replace("\n", "");
            text = text.Replace("\t", "");
            text = text.Replace("'", "’");
            return text;
        }
    }
}
